package com.atm.test;

import com.atm.pojo.User;
import com.atm.utill.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

public class DBUtilsDemo {
    public static void main(String[] args) {
//        test01();
//        test02();
        test03();
    }
    public static void test01(){
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        try {
            User query = queryRunner.query("select * from user where icno = ?", new BeanHandler<>(User.class), "6225123456788");
            System.out.println("query = " + query);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    public static void test02(){
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        try {
            List<User> userList = queryRunner.query("select * from user", new BeanListHandler<>(User.class));
            System.out.println("userList = " + userList);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    public static void test03(){
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        try {
            Object query = queryRunner.query("select count(*) from user", new ScalarHandler<>());
            System.out.println("query = " + query);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
